#pragma once
#include <jsoncpp/json/json.h>
#include "LogMessage.hpp"


class Json_API{
    public:
        static bool serialize(const Json::Value& root, std::string& str)
        {
            Json::StreamWriterBuilder swb;
            std::unique_ptr<Json::StreamWriter> sw(swb.newStreamWriter());
            std::stringstream out_msg;
            int check = sw->write(root, &out_msg);
            if (check != 0){ log_message(ERROR, "消息序列化失败"); return false;}
            str = out_msg.str();
            return true;
        }
    
        static bool deserialize(const std::string& str, Json::Value& root)
        {
            Json::CharReaderBuilder crb;
            std::unique_ptr<Json::CharReader> cr(crb.newCharReader());
            std::string error_msg;
            if (cr->parse(str.c_str(), str.c_str()+str.size(), &root, &error_msg) == false)
            {
                log_message(ERROR, "消息反序列化失败 %s", error_msg.c_str()); 
                return false;
            }
            return true;
        }
    
    };
    